//
//  WmcEditBrandVM.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/9.
//

import RxRelay
import UIKit
import YPBedrock

class WmcEditBrandVM: WmcBaseVM {
    // MARK: 入参

    /// 水印模型
    var watermarkModel: WmcWatermarkDetailModel?

    /// 渲染模式，默认动态，列表实时更新
    /// 静态则根据模型内容进行渲染
    var watermarkStyleMode: WmcBaseWatermarkTemplateView.StyleMode = .dynamicDraw

    // MARK: 输出

    /// 水印模型更新
    let watermarkUpdateRelay: PublishRelay<WmcWatermarkDetailModel> = .init()

    // MARK: 回调

    /// 编辑完成
    var editComplete: YPBlock.OneParam<WmcWatermarkDetailModel>?

    // MARK: 其他

    

    // MARK: 输入

    func loadData() {
        if let watermarkModel = watermarkModel {
            watermarkUpdateRelay.accept(watermarkModel)
        }
    }

    /// 修改图片
    func changeImage(_ image: WmcImage) {
        guard let watermarkModel = watermarkModel,
              let optionBrand = watermarkModel.optionBrand() else { return }
        switch image.imageType {
        case let .url(url):
            let urlStr = String.YP.Create(url)
            optionBrand.content = urlStr
            optionBrand.brand.localImage = nil
        case .uiImage:
            optionBrand.brand.localImage = image
        default: break
        }
        watermarkUpdateRelay.accept(watermarkModel)
    }

    /// 改变大小
    func changeSize(_ size: CGFloat) {
        guard let watermarkModel = watermarkModel,
              let optionBrand = watermarkModel.optionBrand() else { return }
        optionBrand.brand.size = Int(size * 100)
        watermarkUpdateRelay.accept(watermarkModel)
    }

    /// 改变透明度
    func changeAlpha(_ alpha: CGFloat) {
        guard let watermarkModel = watermarkModel,
              let optionBrand = watermarkModel.optionBrand() else { return }
        optionBrand.brand.alpha = Int(alpha * 100)
        watermarkUpdateRelay.accept(watermarkModel)
    }

    /// 改变水印品牌图的位置
    func changeLocationType(_ type: WmcWatermarkOptionModel.Brand.LocationType) {
        guard let watermarkModel = watermarkModel,
              let optionBrand = watermarkModel.optionBrand() else { return }
        optionBrand.brand.location = type.rawValue
        watermarkUpdateRelay.accept(watermarkModel)
    }

    /// 获取水印品牌图的位置
    func locationType() -> WmcWatermarkOptionModel.Brand.LocationType {
        guard let watermarkModel = watermarkModel,
              let optionBrand = watermarkModel.optionBrand() else { return .followWatermark }
        return optionBrand.brand.locationType
    }
}
